package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.Classinfo;
import cn.ytr.crm.mapper.ClassinfoMapper;
import cn.ytr.crm.mapper.EmployeeMapper;
import cn.ytr.crm.query.PageResult;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.IClassinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassinfoServiceImpl implements IClassinfoService{

	@Autowired
	private ClassinfoMapper classinfoMapper;
	@Autowired
	private EmployeeMapper employeeMapper;

	/**
	 * 增加或保存班级
	 */
	@Override
	public void saveOrUpate(Classinfo classinfo) {
		if (classinfo.getId() != null) {
			classinfoMapper.updateByPrimaryKey(classinfo);
		} else {
			classinfoMapper.insert(classinfo);
		}
	}

	/**
	 * 删除班级
	 */
	@Override
	public void delete(Long id) {
		classinfoMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取单个班级
	 */
	@Override
	@Transactional(readOnly=true)
	public Classinfo get(Long id) {
		Classinfo classinfo = classinfoMapper.selectByPrimaryKey(id);
		return classinfo;
	}

	/**
	 * 获取所有班级
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Classinfo> listAll() {
		List<Classinfo> list = classinfoMapper.selectAll();
		return list;
	}

	/**
	 * 高级查询所有班级
	 */
	@Override
	@Transactional(readOnly=true)
	public PageResult query(QueryObject qo) {
		Integer count = classinfoMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<Classinfo> list = classinfoMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}
	}

}
